package com.itheima.controller;

import com.itheima.dto.EmpDto;
import com.itheima.dto.EmpLoginDto;
import com.itheima.pojo.Emp;
import com.itheima.pojo.MyResponseResult;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import com.itheima.utils.MyThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping
    public MyResponseResult<PageBean<Emp>> getByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize, EmpDto empDto){
        PageBean<Emp> pageBean = empService.getByPage(page, pageSize,empDto);
        Map<String, Object> value = MyThreadLocalUtils.getValue();
        System.out.println("============"+value);


        return MyResponseResult.ok(pageBean);
    }

    @DeleteMapping("/{ids}")
    public MyResponseResult<Object> delByIds(@PathVariable Integer[] ids) {
        int i = empService.delByIds(ids);
        return i >= 1 ? MyResponseResult.ok(null) : MyResponseResult.error("error");
    }

    @PostMapping
    public MyResponseResult<Object> insert(@RequestBody Emp emp) {
        int i = empService.insertEmp(emp);
        return i >= 1 ? MyResponseResult.ok(null) : MyResponseResult.error("error");
    }

    @GetMapping("/{id}")
    public MyResponseResult<Object> getById(@PathVariable Integer id){
        Emp emp = empService.getById(id);
        return MyResponseResult.ok(emp);
    }




}
